//
//  GJFMerchantCollectViewController.m
//  Wedding
//
//  Created by Marx on 2017/1/14.
//  Copyright © 2017年 郭静芬. All rights reserved.
//

#import "GJFMerchantCollectViewController.h"
#import "GJFMerchantModel.h"
#import "GJFMerchantTableViewCell.h"
#import "GJFMerchantDetailViewController.h"

@interface GJFMerchantCollectViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *merchantArr;

@end

@implementation GJFMerchantCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"商户收藏";
    [self getMerchantCollectData];
    [self tableView];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-返回"] style:UIBarButtonItemStyleDone target:self action:@selector(popVc)];
}
- (void)popVc {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 网络请求商户页的数据
- (void)getMerchantCollectData {
    [self.view showBusyHUD];
    [GJFNetworkTool POSTRequestWithUrl:APP_MINE_MERCHANTCOLLECT params:@{} complete:^(NSDictionary *result) {
        [self.tableView.mj_header endRefreshing];
        [self.view hideBusyHUD];
        if ([result[@"code"] intValue] == 1000) {
            NSArray *array = result[@"data"];
            self.merchantArr = [GJFMerchantModel transformWithArr:array];
            [self.tableView reloadData];
        }
    }];
}

#pragma mark - TableView的分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
#pragma mark - TableView每个分区中row数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.merchantArr.count;
}
#pragma mark - TableView中cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GJFMerchantTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MerchantCell"];
    cell.merchantModel = self.merchantArr[indexPath.row];
    return cell;
}
#pragma mark - TableView中row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180 * YY;
}
#pragma mark - TableView的跳转方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    GJFMerchantDetailViewController *merDetailVC = [[GJFMerchantDetailViewController alloc] init];
    merDetailVC.hidesBottomBarWhenPushed = YES;
    merDetailVC.mid = [self.merchantArr[indexPath.row] myId];
    [self.navigationController pushViewController:merDetailVC animated:YES];
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[GJFMerchantTableViewCell class] forCellReuseIdentifier:@"MerchantCell"];
        [self.view addSubview:_tableView];
        MJRefreshGifHeader *header = [GJFRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getMerchantCollectData)];
        _tableView.mj_header = header;
        
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
